package com.finance.system.configure.mvc;

import com.finance.system.support.utils.RequestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Component
public class RequestInitFilter implements Filter {


    private final Logger logger = LoggerFactory.getLogger(RequestInitFilter.class);
    @Override
    public void init(FilterConfig filterConfig) {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest servletRequest = (HttpServletRequest) request;

        //设置请求id
        String existRequestId = servletRequest.getHeader(RequestUtils.REQUEST_ID);
        if(existRequestId != null){
            RequestUtils.putRequestId(existRequestId);
        }
        String requestId = RequestUtils.getRequestId();
        MDC.put(RequestUtils.REQUEST_ID,requestId);
        logger.info("请求已进入");
        chain.doFilter(request,response);

    }

    @Override
    public void destroy() {
    }


}
